<?php
/* SVN FILE: $Id: player.php 268 2010-06-21 12:09:09Z gregory.rome $ */
/**
 * Model.
 *
 * @todo Add file description.
 *
 * cBBweb : Digital Blood Bowl league interface (http://code.google.com/p/cbbweb/)
 * Copyright 2009-2010, Grégory Romé.
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2009-2010, Grégory Romé.
 * @link          http://code.google.com/p/cbbweb/ cBBweb Project
 * @package       cbbweb
 * @subpackage    cbbweb.models
 * @version       $Revision: 268 $
 * @modifiedby    $LastChangedBy: gregory.rome $
 * @lastmodified  $Date: 2010-06-21 12:09:09 +0000 (Mon, 21 Jun 2010) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Model.
 *
 * @todo Add model description.
 *
 * @package       cbbweb
 * @subpackage    cbbweb.models
 */
class Player extends AppModel {

	var $name = 'Player';
	var $primaryKey = 'ID';
	var $useTable = 'Player_Listing';

	var $order = array("Player.idTeam_Listing" => "asc", "Player.iNumber" => "asc");

	var $belongsTo = array(
		'PlayerType' => array(
			'className' => 'PlayerType',
			'foreignKey' => 'idPlayer_Types',
			'fields' => array('PlayerType.ID, PlayerType.idStrings_Localized'),
		),
		'Team' => array(
			'className' => 'Team',
			'foreignKey' => 'idTeam_Listing',
			'fields' => array('Team.ID, Team.strName'),
		),
		'Race' => array(
			'className' => 'Race',
			'foreignKey' => 'idRaces',
			'fields' => array('Race.ID, Race.idStrings_Localized'),
		),
		'PlayerLevel' => array(
			'className' => 'PlayerLevel',
			'foreignKey' => 'idPlayer_Levels',
			'fields' => array('PlayerLevel.ID', 'PlayerLevel.idStrings_Localized'),
		)
	);

	var $hasOne = array(
		'StatisticsPlayer' => array(
			'className' => 'StatisticsPlayer',
			'foreignKey' => 'idPlayer_Listing',
			'dependent' => true
		),
		'StatisticsCurrentSeasonPlayer' => array(
			'className' => 'StatisticsSeasonPlayer',
			'foreignKey' => 'idPlayer_Listing',
			'conditions'	=> array('StatisticsCurrentSeasonPlayer.bFinished' => 0),
			'dependent' => false
		)
	);

	var $hasMany = array(
		'PlayerSkill' => array(
			'className' => 'PlayerSkill',
			'foreignKey' => 'idPlayer_Listing',
			'dependent' => true
		),
		'StatisticsSeasonPlayer' => array(
			'className' => 'StatisticsSeasonPlayer',
			'foreignKey' => 'idPlayer_Listing',
			'dependent' => false
		)
	);

	var $hasAndBelongsToMany = array(
		'Skill' => array(
			'className' => 'Skill',
			'joinTable' => 'Player_Skills',
			'foreignKey' => 'idPlayer_Listing',
			'associationForeignKey' => 'idSkill_Listing',
			'unique' => true
		)
	);

	function afterFind($results, $primary)
	{
		if(!$primary) return $results;
		if(!$results) return $results;

		foreach ($results as $key => $val) {

			if (isset($val['Player']['Characteristics_fMovementAllowance'])) {
				$results[$key]['Player']['Characteristics_fMovementAllowance'] = $this->convertCharac($val['Player']['Characteristics_fMovementAllowance'], 'mov');
			}

			if (isset($val['Player']['Characteristics_fStrength'])) {
				$results[$key]['Player']['Characteristics_fStrength'] = $this->convertCharac($val['Player']['Characteristics_fStrength'],'str');
			}

			if (isset($val['Player']['Characteristics_fAgility'])) {
				$results[$key]['Player']['Characteristics_fAgility'] = $this->convertCharac($val['Player']['Characteristics_fAgility'],'agi');
			}

			if (isset($val['Player']['Characteristics_fArmourValue'])) {
				$results[$key]['Player']['Characteristics_fArmourValue'] = $this->convertCharac($val['Player']['Characteristics_fArmourValue'],'arm');
			}
		}
		return $results;
	}

	private function convertCharac($val, $type)
	{
		if($val <= 10) return round($val);

		switch ( $type ) {
			case 'mov':
				$ret = 6 + round( (($val-50) / 8.3330001831055));
				break;

			case 'str':
				$ret = 3 + round( (($val-50) / 10));
				break;

			case 'agi':
				$ret = 3 + round( (($val-50) / 16.6660003662109));
				break;

			case 'arm':
				if ( 41.67 >= round($val,2) ) {
					$ret = 6;
				} else if ( 58.33 == round($val,2) ) {
					$ret = 7;
				} else if ( 72.22 == round($val,2) ) {
					$ret = 8;
				} else if ( 83.33 == round($val,2) ) {
					$ret = 9;
				} else if ( 91.66 <= round($val,2) ) {
					$ret = 10;
				} else { $ret = round($val,2); }

				break;
		}

		return $ret;
	}

}
?>